package org.dromara.web.utils;

import cn.hutool.core.util.StrUtil;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;

public class SendMsg {
    public static int msg(String phoneNum,String code) throws Exception {
        String url = "https://139.9.32.119:18312/common/sms/sendTemplateMessage";
        int responseCode = 500;
            String request = "{\n" +
            "    \"account\": \"970052\",\n" +
            "    \"password\": \"Ge%;5fY_+'$@\\\"/ch\",\n" +
            "\n" +
            "\"requestLists\": \n" +
            "[\n" +
            "        {\n" +
            "            \"mobiles\": [\n" +
            "                \"{}\"\n" +
            "            ],\n" +
            "            \"templateId\": \"SMS_24070800002\",\n" +
            "            \"templateParas\": {\n" +
            "                \"code\": \"{}\"\n" +
            "            },\n" +
            "            \"signature\":\"【漫元】\"\n" +
            "        }\n" +
            "]\n" +
            "}";
        String requestBody = StrUtil.format(request, phoneNum, code);
        URL apiUrl = new URL(url);
        SslUtils.ignoreSsl();
        HttpsURLConnection connection = (HttpsURLConnection) apiUrl.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        try {
            SslUtils.ignoreSsl();
            OutputStream outputStream = connection.getOutputStream();
            SslUtils.ignoreSsl();
            outputStream.write(requestBody.getBytes());
            outputStream.flush();
        }catch (Exception e){
            try {
                SslUtils.ignoreSsl();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                System.out.println("utils");
            }
            e.printStackTrace();

        }
        try  {
            responseCode = connection.getResponseCode();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

            reader.close();
            connection.disconnect();


        }catch (Exception e){
            try {
                SslUtils.ignoreSsl();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                System.out.println("utils");
            }
            e.printStackTrace();

        }
        return responseCode;
    }
}
